package com.example.wanbaqian.fragment;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.example.wanbaqian.R;
import com.example.wanbaqian.activity.WebViewActivity;
import com.example.wanbaqian.adapter.FindMessageAdapter;
import com.example.wanbaqian.base.BaseFragment;
import com.example.wanbaqian.config.CommonValue;
import com.example.wanbaqian.base.HttpCallback;
import com.example.wanbaqian.config.HttpURL;
import com.example.wanbaqian.base.RefreshCallback;
import com.example.wanbaqian.bean.Info;
import com.example.wanbaqian.bean.NoticeInfo;
import com.example.wanbaqian.utils.GetJsonUtils;
import com.example.wanbaqian.utils.HttpUtils;
import com.example.wanbaqian.utils.JsonUtils;
import com.example.wanbaqian.utils.SPUtils;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.wrapper.EmptyWrapper;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 消息公告fragment.
 */
public class FindMessageFragment extends BaseFragment implements HttpCallback, RefreshCallback {
    private static final String ARG_PARAM1 = "param1";

    @BindView(R.id.mRecyclerView)
    RecyclerView mRecyclerView;
    private List<NoticeInfo> mList = new ArrayList<>();
    private EmptyWrapper mAdapter;
    private FindMessageAdapter adapter;

    public static FindMessageFragment newInstance(String param1) {
        FindMessageFragment fragment = new FindMessageFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.fragment_findlist;
    }

    @Override
    protected void initViewsAndEvents(View view) {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        adapter = new FindMessageAdapter(mContext, R.layout.item_findmessage, mList);
        mAdapter = new EmptyWrapper(adapter);
        View emptyView = getActivity().getLayoutInflater().inflate(R.layout.view_empty_activits, mRecyclerView, false);
        TextView emptyTv = ((TextView) emptyView.findViewById(R.id.emptyTv));
        emptyTv.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(getActivity(), R.drawable.empty_message), null, null);
        emptyTv.setText("暂无公告~");
        mAdapter.setEmptyView(emptyView);
        mRecyclerView.setAdapter(mAdapter);
        adapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                Bundle bundle2 = new Bundle();
                bundle2.putString("title", "消息公告详情");
                bundle2.putString("url", "http://wbq.yirongw.com/discover/affiche.do?notice_id=" + mList.get(position).getNotice_id());
                openActivity(WebViewActivity.class, bundle2);
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
        setHeaderAndFooter(R.id.springview, true, true, this);
    }

    @Override
    protected void onFirstUserVisible() {
        getDate();
    }

    private void getDate() {
        HttpUtils.getInstance().post(GetJsonUtils.getInstance()
                        .getRequest(HttpURL.LANMEI_NOTICE, mContext,
                                new String[]{"accessid", "cur_page", "page_size"}
                                , new Object[]{SPUtils.getAccessid(mContext), page,
                                        count})
                , HttpURL.LANMEI_NOTICE, CommonValue.REQUEST_CODE1, this);
    }

    @Override
    protected void onUserVisible() {

    }

    @Override
    protected void onUserInvisible() {

    }

    @Override
    protected void DetoryViewAndThing() {

    }

    @Override
    public void onSuccess(String content, Info info, int requestCode) {
        springView.onFinishFreshAndLoad();
        if (page == 1) {
            mList.clear();
        }
        canLoadmore = JsonUtils.getInstance().havePage(content);
        List<NoticeInfo> newmList = JsonUtils.getInstance().getList(content, "notice_list", NoticeInfo.class);
        mList.addAll(newmList);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onCacheSuccess(String content, Info info, int requestCode) {

    }

    @Override
    public void onError(Info info, int requestCode) {
        springView.onFinishFreshAndLoad();
        showToast(info.getMsg());
    }
    @Override
    public void getNetDate() {
        getDate();
    }
}
